#region Using Statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace _1942
{
    /// <summary>
    /// This game component implements a manager for all Kamikaze planes in the game.
    /// </summary>
    public class KamikazeManager : DrawableGameComponent
    {
        #region Variables

        // List of active planes
        protected List<Kamikaze> kamikazes;

        // Constant for initialplane count
        private const int START_NUM_OF_KAMIKAZE = 1;

        // Time for a new plane
        private const int ADD_KAMIKAZE_TIME = 12000;

        // Audio
        private AudioLibrary audio;

        protected Texture2D planeTexture;
        protected TimeSpan elapsedTime = TimeSpan.Zero;

        private Color[] texturesData;
			 
        //If the planes should reappear
        private bool shouldSpawn = true;

        #endregion

        #region Constructors

        public KamikazeManager(Game game, ref Texture2D theTexture)
            : base(game)
        {
            planeTexture = theTexture;
            kamikazes = new List<Kamikaze>();

            // Get the audio library
            audio = (AudioLibrary)Game.Services.GetService(typeof(AudioLibrary));

            Rectangle frame = new Rectangle(
        Sprite.START_OFFSET + 32 * 3 + 2,
        Sprite.START_OFFSET + 32 * 14 + 13,
        32,
        32);

            texturesData = new Color[frame.Width * frame.Height];
            theTexture.GetData<Color>(0, frame, texturesData, 0, frame.Width * frame.Height);

        }

        #endregion

        #region Methods

        /// <summary>
        /// Allows the game component to perform any initialization it needs to 
        /// before starting to run.  This is where it can query for any required
        /// services and load content.
        /// </summary>
        public override void Initialize()
        {							  
            kamikazes.Clear();

            // Initialize a counter
            elapsedTime = TimeSpan.Zero;

            // Add the planes
            for (int i = 0; i < START_NUM_OF_KAMIKAZE; i++)
            {
                AddNewKamikaze();
            }

            base.Initialize();
        }

        protected override void UnloadContent()
        {
            this.planeTexture.Dispose();
            base.UnloadContent();
        }

        /// <summary>
        /// Add a new plane in the scene
        /// </summary>
        private Kamikaze AddNewKamikaze()
        {
            Kamikaze newKamikaze = new Kamikaze(Game, ref planeTexture);

            newKamikaze.Initialize();

            kamikazes.Add(newKamikaze);

            return newKamikaze;
        }

        /// <summary>
        /// Check if is a moment for a new kamikaze
        /// </summary>
        private void CheckforNewKamikaze(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime > TimeSpan.FromMilliseconds(ADD_KAMIKAZE_TIME))
            {
                elapsedTime -= TimeSpan.FromMilliseconds(ADD_KAMIKAZE_TIME);
                AddNewKamikaze();
            }
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if(shouldSpawn) CheckforNewKamikaze(gameTime);

            // Update Planes
            foreach (Kamikaze item in kamikazes)
            {
                if (item.Enabled)
                    item.Update(gameTime);
            }

            kamikazes.RemoveAll(delegate(Kamikaze item)
            {
                if (!item.Enabled)
                    return true;
                return false;
            }
            );

            base.Update(gameTime);
        }

        /// <summary>
        /// Allows the game component draw your content in game screen
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            // Draw the planes
            foreach (Kamikaze item in kamikazes)
            {
                if ( item.Visible )
                    item.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

        #endregion

        #region Properties

        /// <summary>
        /// All kamikazess in the game
        /// </summary>
        public List<Kamikaze> AllKamikaze
        {
            get { return kamikazes; }
        }

        public Color[] TextureData
        {
            get { return texturesData; }
        }
			  
       public bool ShouldSpawn
        {
            get { return shouldSpawn; }
            set
            {
                shouldSpawn = value;
                foreach (Kamikaze k in kamikazes)
                {
                    k.ShouldSpawn = false;
                }
            }
        }
        #endregion
    }
}